# class base for controller
from google.appengine.ext import webapp

# needed for display
from tools import language_manager

class Select(webapp.RequestHandler):
    '''
    '''

    def post(self):
        '''
        '''
        # get the key to edit
        language_key = self.request.get('language_key')
        #
        size_key = self.request.get('size_key')
        #
        la_manager = language_manager.LanguageManager(
                language_key=language_key, size_key=size_key)

        # get the key to edit
        select_language_key = self.request.get('select_language_key')
        language = la_manager.get_language(select_language_key)

        # get the current action
        action = self.request.get('action')

        # get the current page_id
        page_id = self.request.get('page_id')

        # back to the crre t page
        return self.redirect('/%s?language=%s&size=%s&page_id=%s' % (action,
            language.locale_id, la_manager.size.width, page_id))

class Edit(webapp.RequestHandler):
    '''
    '''

    def post(self):
        '''
        '''
        # get the key to edit
        language_key = self.request.get('language_key')
        #
        locale_id = self.request.get('locale_id')
        #
        size_key = self.request.get('size_key')
        #
        la_manager = language_manager.LanguageManager(
                language_key=language_key, size_key=size_key)
        #
        locale_id = self.request.get('locale_id')

        # display the language to edit
        template = la_manager.edit(locale_id=locale_id)
        return self.response.out.write(template)

class Save(webapp.RequestHandler):
    '''
    '''

    def post(self):
        '''
        '''
        # get the key to edit
        language_key = self.request.get('language_key')
        #
        size_key = self.request.get('size_key')
        #
        la_manager = language_manager.LanguageManager(
                language_key=language_key, size_key=size_key)

        language_to_edit_key = self.request.get('language_to_edit_key')

        # delete
        if not self.request.get('delete') == '':
            # get the new key to edit
            la_manager.delete(language_to_edit_key)

            # go to the index page
            return self.redirect('/?language=%s&size=%s' % (
                la_manager.language.locale_id,
                la_manager.size.width))

        # cancel
        elif not self.request.get('new') == '':
            # get the new key to edit
            language_to_edit_key = la_manager.new_language()

        # cancel
        elif not self.request.get('cancel') == '':
            # go to the index page
            return self.redirect('/?language=%s&size=%s' % (
                la_manager.language.locale_id,
                la_manager.size.width))

        # we save and display the language
        else:
            # get id
            locale_id = self.request.get('locale_id')
            # get name
            name = self.request.get('name')
            # get default
            default = self.request.get('default') == 'on'
            # tools save the language
            la_manager.save(
                    language_to_edit_key, locale_id, name, default)

        # display the language to edit
        template = la_manager.edit(language_to_edit_key)
        return self.response.out.write(template)
